package br.ufrgs.dbproject.algorithm.epns;

import br.ufrgs.dbproject.algorithm.IDStrategy;

/**
 * 
 * @author diego
 *
 */
public class NumberingSchemeID implements IDStrategy {

	private final long order;
	private long size;

	private NumberingSchemeID lastCreatedChild;
	
	protected NumberingSchemeID(long order, long size) {
		
		this.order = order;
		this.size = size;
	}

	/**
	 * Creates a new child ID for this node.
	 * The child ID is created based on the last created one. If this is the first
	 * child being created, then the lastCreatedChild is equals to {@link EmptyNumberingSchemeID}.
	 */
	public NumberingSchemeID createChild() {
		
		NumberingSchemeID child = new NumberingSchemeID(lastCreatedChild.getOrder() + lastCreatedChild.getSize()
				+ NumberingSchemeIDFactory.ID_ORDER_DISPLACEMENT, NumberingSchemeIDFactory.ID_DEFAULT_SIZE);
		
		lastCreatedChild = child;
		
		return child;
	}
	
	public void merge(NumberingSchemeID childID) {
		this.size += childID.getSize();		
	}
	
	protected void setLastCreatedChild(NumberingSchemeID lastCreatedChild) {
		this.lastCreatedChild = lastCreatedChild;
	}
	
	protected NumberingSchemeID getLastCreatedChild() {
		return lastCreatedChild;
	}

	public long getOrder() {
		return order;
	}

	public long getSize() {
		return size;
	}

}